package com.googlecode.afdbaccess.web.common.errorhelp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

import org.apache.log4j.Logger;

import com.googlecode.afdbaccess.web.common.util.HtmlEscaper;


public class StacktraceToHtml {
    private static final Logger log = Logger.getLogger(StacktraceToHtml.class);
    
    public static String makeHtmlFromStacktraceString(String stackTraceString) {
        String result = "";
        
        BufferedReader reader = new BufferedReader(new StringReader(stackTraceString));
    
        try {
            String line = reader.readLine();
            
            boolean nextStrong = false;
    
            while(line != null) {
                boolean thisStrong = nextStrong;
                nextStrong = false;
                
                if(result.equals("") == false) {
                    result += "\n";
                }
                
                String escapedLine = HtmlEscaper.escapeHtml(line);
                
                if(line.startsWith("\tat ") || line.startsWith("\t...") && line.trim().endsWith("more")) {
                    if(line.indexOf(".jsp:") >= 0) {
                        escapedLine = escapedLine.replaceAll("\\(", "(<span style=\"background-color: yellow\">").replaceAll("\\)", "</span>)");
                        result += "<span style=\"color: black; font-weight: bold; font-size: larger\">" + escapedLine + "</span>";
                    } else if(line.indexOf("_jsp.java:") >= 0 /*&& line.indexOf("_jspService(") >= 0*/) {
                            escapedLine = escapedLine.replaceAll("\\(", "(<span style=\"background-color: lightgray\">").replaceAll("\\)", "</span>)");
                            result += "<span style=\"color: black; font-weight: bold\">" + escapedLine + "</span>";
                    } else if(line.indexOf("com.googlecode.afdbaccess.cache.TransactionBase.execute") >= 0) {
                        result += escapedLine;
                        
                        nextStrong = true;
                    } else if(line.trim().startsWith("at com.googlecode.afdbaccess.cache.")) {
                        result += escapedLine;
                    } else if(line.indexOf(".convertResultSetToData(") >= 0) {
                        escapedLine = escapedLine.replaceAll(".convertResultSetToData\\(", ".<span style=\"background-color: yellow\">convertResultSetToData</span>(");
                        escapedLine = escapedLine.replaceAll("\\(", "(<span style=\"background-color: yellow\">").replaceAll("\\)", "</span>)");
                        result += "<span style=\"color: green; font-weight: bold; font-size: larger\">" + escapedLine + "</span>";
                    } else if(line.indexOf(".convertSingleRowToData(") >= 0) {
                        escapedLine = escapedLine.replaceAll(".convertSingleRowToData\\(", ".<span style=\"background-color: yellow\">convertSingleRowToData</span>(");
                        escapedLine = escapedLine.replaceAll("\\(", "(<span style=\"background-color: yellow\">").replaceAll("\\)", "</span>)");
                        result += "<span style=\"color: green; font-weight: bold; font-size: larger\">" + escapedLine + "</span>";
                    } else if(line.trim().startsWith("at com.googlecode.afdbaccess.")) {
                        if(thisStrong) {
                            escapedLine = escapedLine.replaceAll("\\(", "(<span style=\"background-color: yellow\">").replaceAll("\\)", "</span>)");
                            escapedLine = "<span style=\"font-weight: bold; font-size: larger\">" + escapedLine + "</span>";
                        }
                        result += "<span style=\"color: green\">" + escapedLine + "</span>";
                    } else if(line.trim().startsWith("at ")) {
                        result += "<span style=\"color: #aaaaaa\">" + escapedLine + "</span>";
                    } else {
                        result += escapedLine;
                    }
                } else {
                    result += "<span class=\"errorheader\">" + escapedLine + "</span>";
                }
    
                line = reader.readLine();
            }
        } catch (IOException ioException) {
            log.debug("Exception when making html from stacktrace string. Swallowed and worked around by returning the text.", ioException);
            result = stackTraceString;
        }
        
        return result;
    }
}
